<!-- BEGIN PAGE HEADER-->
<?php $this->load->view('partials/breadcrumb.tpl') ?>
<div class="row">
    <div class="col-xs-12">
        <!-- Begin: life time stats -->
        <div class="portlet">
            <?= form_open('', Array('class' => 'form-inline', 'role' => 'form')) ?>
            <?= search_form_dropdown('shop_id', array('' => '选择账户...') + $allShops, filterValue('shop_id'), '所属账户', 'class="form-control input-xs select2me new-select"'); ?>
            <?= search_form_input('shipping_no', filterValue('shipping_no'), '物流单号', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('location', array('' => '选择发货仓库...') + IOSS_StaticConfig::getShippingWarehouse(), filterValue('location'), '选择发货仓库', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('status', array('' => '状态...') + MStorageShipping::getState(), filterValue('status'), '状态', 'class="form-control input-xs"'); ?>
            <div style="clear:both;height:14px;"></div>
            <?= search_form_input('record_number_perpage', filterValue('shipping_number_perpage'), '每页显示条数', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('shipping_order_type', array('' => '排序方式...') + MStorageShipping::getOrderType(), filterValue('shipping_order_type'), '排序', 'class="form-control input-xs"'); ?>
            <div class="input-group input-large date-picker input-daterange" data-date="" data-date-format="yyyy-mm-dd">
                <input type="text" class="form-control" name="shipping_create_time_begin"
                       value="<?= filterValue('shipping_create_time_begin') ?>" placeholder="开始时间" autoComplete="off">
                <span class="input-group-addon">to </span>
                <input type="text" class="form-control" name="shipping_create_time_end"
                       value="<?= filterValue('shipping_create_time_end') ?>" placeholder="结束时间" autoComplete="off">
            </div>

            <button type="button" class="btn default" id="reset">重置条件</button>
            <button type="submit" class="btn green about_search">搜索 <i class="fa fa-search"></i></button>

            <?= form_close() ?>
            <div class="portlet-body">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="table-scrollable">
                            <table class="table table-striped table-bordered table-hover">
                                <thead class="flip-content">
                                <tr>
                                    <th>ID</th>
                                    <th>发货日期</th>
                                    <th> 所属账户</th>
                                    <th>运单号</th>
                                    <th>发货方式</th>
                                    <th>发货人</th>
                                    <th>发货仓库</th>
                                    <th>成本</th>
                                    <th>打包箱数</th>
                                    <th>运费</th>
                                    <th>状态</th>
                                    <th>备注</th>
                                    <th>创建时间</th>
                                    <th>修改历史</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php foreach ($list as $v) { ?>
                                    <tr>
                                        <td><?= $v->id ?></td>
                                        <td><?= date('Y-m-d', $v->shipping_time) ?></td>
                                        <td><?= $allShops[$v->shop_id] ?></td>
                                        <td><?= $v->shipping_number ?></td>
                                        <td><?= htmlspecialchars($v->shipping_desc) ?></td>
                                        <td><?= $allPurchaser[$v->operator_id] ?></td>

                                        <td><?= $v->location ? IOSS_StaticConfig::getShippingWarehouse($v->location) : '未入库' ?></td>
                                        <td><?= $v->cost ?></td>
                                        <td><?= $v->boxs ?></td>
                                        <td><?= $v->freight ?></td>
                                        <td><?= $v->status ? '<span class="label label-success">成功</span>' : '<span class="label label-danger">作废</span>' ?></td>
                                        <td><?= htmlspecialchars($v->remark) ?></td>
                                        <td><?= date('Y-m-d H:i', $v->create_time) ?></td>
                                        <td> <button data-id="<?= $v->id ?>"
                                                     data-route="<?= $this->_thisModule . $this->_thisController . '/edit' ?>"
                                                     class="btn btn-sm btn-success edit-his">修改记录
                                            </button></td>
                                        <td class="numeric text-center">
                                            <div class="btn-group btn-group-solid">
                                                <a href="<?= site_url($thisModule . $thisController . '/view/' . $v->id) ?>"
                                                   class="btn btn-sm blue <?= $p->read ? '' : 'disabled' ?>">查看</a>

                                                <?php if ($v->status): ?>
                                                    <button type="button" data-id="<?= $v->id ?>"
                                                            class="btn btn-sm purple record-edit">编辑
                                                    </button>
                                                    <a href="javascript:void(defConfirm('<?= site_url($thisModule . $thisController . '/shipping_fail/' . $v->id) ?>'));"
                                                       class="btn btn-sm btn-warning">作废</a>
                                                <?php endif; ?>

                                            </div>
                                        </td>
                                    </tr>
                                <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12 text-right">
                        <?= $pagination ?>
                    </div>
                </div>
            </div>
        </div>
        <!-- End: life time stats -->
    </div>
</div>

<script>

    var editHis = $(".edit-his");
    editHis.click(function () {
        var id = this.dataset.id;
        var route = this.dataset.route;
        $.ajax({
            url: "<?= site_url('storage/warehouse/ajaxHis') ?>",
            method: "POST",
            data: {'id': id, 'route': route},
            dataType: "json",
            success: function (res) {
                console.log(res);
                var html = '';
                if (!res.length) {
                    toastr.error('暂无修改记录！', '暂无')
                } else {
                    for (var i = 0; i < res.length; i++) {
                        html += '<div class=""><p class="bg-warning" style="padding: 2px 4px;"> <span> '+res[i].user_name+' </span>于 <span style="color:#2120ff">'+res[i].create_time+'</span> <span>操作：</span><span style="color:red;">'+res[i].content+'</span><p></div>'
                    }
                    $("#log-content").html(html);
                    $('.logModal').modal('show');
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    });

    var recordEdit = $(".record-edit");
    recordEdit.click(function () {
        var record_id = this.dataset.id;
        $.ajax({
            url: "<?= site_url('storage/shipping/ajaxDetail') ?>",
            method: "POST",
            data: {'id': record_id},
            dataType: "json",
            success: function (res) {
                document.getElementById("form_edit").action = "<?= site_url('/storage/shipping/edit/') ?>" + '/' + record_id;
                set_select_checked('shop-id', res.shop_id);
                set_select_checked('location', res.location);

                $("input[name='shipping_number']").val(res.shipping_number);
                $("input[name='shipping_desc']").val(res.shipping_desc);
                $("input[name='cost']").val(parseFloat(res.cost));
                $("input[name='freight']").val(parseFloat(res.freight));
                $("input[name='boxs']").val(res.boxs);
                $("input[name='shipping_time']").val(res.shipping_time);
                $("textarea[name='remark']").val(res.remark);
            },
            fail: function (res) {
                console.log(res);
            }
        });
        $(".editModal").modal('show');
    });

    function set_select_checked(selectId, checkValue) {
        var select = document.getElementById(selectId);
        for (var i = 0; i < select.options.length; i++) {
            if (select.options[i].value == checkValue) {
                select.options[i].selected = true;
                break;
            }
        }
    }
</script>

<!--  log-modal  -->
<div class="modal fade logModal">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="gridModalLabel">操作记录</h4>
            </div>
            <div class="modal-body" id="log-content">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
            <?= form_close() ?>
        </div>
    </div>
</div>

<!--  edit modal  -->
<div class="modal fade editModal">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <?= form_open(site_url(''), Array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form_edit')) ?>
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="gridModalLabel">编辑</h4>
            </div>
            <div class="modal-body">

                <?= edit_form_input('shipping_number', set_value('shipping_number'), '物流单号') ?>
                <?= edit_form_dropdown('shop_id', array('' => '选择账户组...') + $allShops, set_value('shop_id'), '选择账户组', "id='shop-id' style='width:100%;'") ?>
                <?= edit_form_dropdown('location', array('' => '选择仓库...') + IOSS_StaticConfig::getShippingWarehouse(), set_value('location'), '发货仓库', "id='location'") ?>
                <?= edit_form_input('shipping_desc', set_value('shipping_desc'), '发货方式') ?>
                <?= edit_form_input('cost', set_value('cost'), '成本', 'readonly') ?>
                <?= edit_form_input('boxs', set_value('boxs'), '打包箱数', 'required') ?>
                <?= edit_form_input('freight', set_value('freight'), '运费', 'required') ?>

                <div class="form-group">
                    <label class="col-xs-3 control-label">发货日期</label>
                    <div class="col-xs-7">
                        <div class="input-group input-large date-picker input-daterange" data-date=""
                             data-date-format="yyyy-mm-dd">
                            <input type="text" class="form-control" required name="shipping_time"
                                   value="<?= set_value('shipping_time') ?>" placeholder="发货日期">
                        </div>
                    </div>
                </div>
                <?= edit_form_textarea('remark', set_value('remark'), '备注信息') ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="submit" class="btn btn-primary ajaxSub">保存</button>
            </div>
            <?= form_close() ?>
        </div>
    </div>
</div>

<!-- END PAGE HEADER-->
<script type="text/javascript">
    function defConfirm(url) {
        if (!confirm("此操作会将此物流信息内所有出库记录变更为待发货，确认要讲本条物流作废吗？")) {
            return false;
        }
        window.location.href = url;
        return true;
    }

    $(document).ready(function () {
        $("#reset").click(function () {
            var SelectArr = $("select");
            var SelectShop = $("select[name='shop_id']");
            var SelectProduct = $("select[name='product_id']");
            $("input[type='text']").val('');
            SelectArr.val('');

            var shop = SelectShop[0].options[0].text;
            var product = SelectProduct[0].options[0].text;
            var v_span = $('.select2-chosen');
            v_span.eq(0).html(shop);
            v_span.eq(1).html(product);

            for (var i = 0; i < SelectArr.length; i++) {
                var opt = SelectArr[i].options[0];
                opt.selected = true;

            }
        });
    });
</script>